﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Diagnostics;

namespace _1945
{
    public class StartScene : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;
        KeyboardState keyboardState;
        KeyboardState previousKeyboardState = new KeyboardState(Keys.W);

        Color itemColor;
        float itemTextScaling = 2;
        float itemHeight = 30;
        float itemWidth = 160;
        int selectedItem = 0;
        Vector2 center;
        Vector2 position;
        string[] items = new string[] { "Play 1945", "View highscore", "Exit" };
        Texture2D gameSpriteSheet;

        public StartScene(Game1 game, Texture2D gameSpriteSheet)
            : base(game)
        {
            this.gameSpriteSheet = gameSpriteSheet;

            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            spriteFont = (SpriteFont)Game.Services.GetService(typeof(SpriteFont));

            itemHeight *= itemTextScaling;
            itemWidth *= itemTextScaling;
            center = new Vector2(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Up) && !previousKeyboardState.IsKeyDown(Keys.Up))
            {
                selectedItem -= 1;

                if (selectedItem < 0)
                {
                    selectedItem = items.Count() -1;
                }
            }
            else if (keyboardState.IsKeyDown(Keys.Down) && !previousKeyboardState.IsKeyDown(Keys.Down))
            {
                selectedItem +=1;

                if (selectedItem == items.Count())
                {
                    selectedItem  = 0;
                }
            }
            else if (keyboardState.IsKeyDown(Keys.Enter))
            {
                switch (selectedItem)
                {
                    case 0:
                        ((Game1)Game).setGameScene(Game1.GameScenes.gameScene);
                        break;
                    case 1:
                        ((Game1)Game).setGameScene(Game1.GameScenes.scoreScene);
                        break;
                    case 2:
                        Game.Exit();
                        break;
                }

            }

            previousKeyboardState = Keyboard.GetState();
            base.Update(gameTime);
        }

        /// <summary>
        /// Called when the DrawableGameComponent needs to be drawn. Override this method with component-specific drawing code.
        /// Reference page contains links to related conceptual articles.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to Microsoft.Xna.Framework.DrawableGameComponent.Draw(Microsoft.Xna.Framework.GameTime).</param>
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            Rectangle gamePoster = SpriteFactory.getBigGamePoster();
            //regular 1945 image
            //spriteBatch.Draw(gameSpriteSheet, new Vector2(Game.Window.ClientBounds.Width / 2 - gamePoster. Width/2, 50), gamePoster, Color.White);
            
            //scaled 1945 image
            float scale = 3;
            spriteBatch.Draw(gameSpriteSheet, new Vector2(Game.Window.ClientBounds.Width / 2 - gamePoster.Width * scale / 2, 50), gamePoster, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
            
            //fullscreen 1945 image
            //spriteBatch.Draw(gameSpriteSheet, new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height), r, Color.White); 

            //spriteBatch.DrawString(spriteFont, "1945", new Vector2(center.X -180, 100), Color.Yellow, 0, Vector2.Zero, new Vector2(4), SpriteEffects.None, 0);

            for (int i = 0; i < items.Count(); i++)
            {
                if (i == selectedItem)
                {
                    itemColor = Color.Blue;
                }
                else
                {
                    itemColor = Color.White;
                }

                position = new Vector2(center.X - itemWidth / 2, center.Y - items.Count() * itemHeight / 2 + i * itemHeight + 225);
                spriteBatch.DrawString(spriteFont, items[i], position, itemColor, 0, Vector2.Zero, new Vector2(itemTextScaling), SpriteEffects.None, 0);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }


    }
}
